package us.icitap.cm.utils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import us.icitap.cm.CaseManagementUI;
import us.icitap.cm.business.AppListTypeEnum;
import us.icitap.cm.business.entities.ActualSituation;
import us.icitap.cm.business.entities.AppList;
import us.icitap.cm.business.entities.regmin.Color;
import us.icitap.cm.business.entities.regmin.Region;
import us.icitap.cm.business.entities.regmin.States;
import us.icitap.cm.business.entities.regmin.VehicleBrand;
import us.icitap.cm.business.entities.regmin.VehicleType;


public class ProjectLists {

	private static List<States> statesList;
	private static  Date statesListDate;
	
	@SuppressWarnings("unchecked")
	public static  List<States> getStatesList(boolean freshData) {
		Date now = new Date();
		if (statesList == null || statesListDate == null || freshData){
			statesList =  CaseManagementUI.appStatelessBean.getPersistenceBean().getList(States.class, false, "o.tag").getObject();
			
			statesListDate = now;
			
		}
		int difHours = (int)((now.getTime() - statesListDate.getTime()) / (1000*60*60));
		if (difHours > 24){
			statesList =  CaseManagementUI.appStatelessBean.getPersistenceBean().getList(States.class, false, "o.tag").getObject();
			statesListDate = now;
			
		}
		
		return statesList;
	}
	

	

	
	private static List<Region> regionList;
	private static  Date regionDate;
	
	@SuppressWarnings("unchecked")
	public static  List<Region> getRegionList(boolean freshData) {
		Date now = new Date();
		if (regionList == null || regionDate == null || freshData){
			regionList =  CaseManagementUI.appStatelessBean.getPersistenceBean().getList(Region.class, false, "o.tag").getObject();
			
			regionDate = now;
			
		}
		int difHours = (int)((now.getTime() - regionDate.getTime()) / (1000*60*60));
		if (difHours > 24){
			regionList =  CaseManagementUI.appStatelessBean.getPersistenceBean().getList(Region.class, false, "o.tag").getObject();
			regionDate = now;
			
		}
		
		return regionList;
	}
	
	
	private static List<VehicleType> vehicleTypeList;
	private static  Date vehicleTypeDate;
	
	@SuppressWarnings("unchecked")
	public static  List<VehicleType> getVehicleTypeList(boolean freshData) {
		Date now = new Date();
		if (vehicleTypeList == null || vehicleTypeDate == null || freshData){
			vehicleTypeList =  CaseManagementUI.appStatelessBean.getPersistenceBean().getList(VehicleType.class, false, "o.tag").getObject();
			
			vehicleTypeDate = now;
			
		}
		int difHours = (int)((now.getTime() - vehicleTypeDate.getTime()) / (1000*60*60));
		if (difHours > 24){
			vehicleTypeList =  CaseManagementUI.appStatelessBean.getPersistenceBean().getList(VehicleType.class, false, "o.tag").getObject();
			vehicleTypeDate = now;
		}
		return vehicleTypeList;
	}
	
	private static List<VehicleBrand> vehicleBrandList;
	private static  Date vehicleBrandDate;
	
	@SuppressWarnings("unchecked")
	public static  List<VehicleBrand> getVehicleBrandList(boolean freshData) {
		Date now = new Date();
		if (vehicleBrandList == null || vehicleBrandDate == null || freshData){
			vehicleBrandList =  CaseManagementUI.appStatelessBean.getPersistenceBean().getList(VehicleBrand.class, false, "o.tag").getObject();
			
			vehicleBrandDate = now;
			
		}
		int difHours = (int)((now.getTime() - vehicleBrandDate.getTime()) / (1000*60*60));
		if (difHours > 24){
			vehicleBrandList =  CaseManagementUI.appStatelessBean.getPersistenceBean().getList(VehicleBrand.class, false, "o.tag").getObject();
			vehicleBrandDate = now;
		}
		return vehicleBrandList;
	}
	
	
	private static List<Color> colorList;
	private static  Date colorDate;
	
	@SuppressWarnings("unchecked")
	public static  List<Color> getColorList(boolean freshData) {
		Date now = new Date();
		if (colorList == null || colorDate == null || freshData){
			colorList =  CaseManagementUI.appStatelessBean.getPersistenceBean().getList(Color.class, false, "o.tag").getObject();
			
			colorDate = now;
			
		}
		int difHours = (int)((now.getTime() - colorDate.getTime()) / (1000*60*60));
		if (difHours > 24){
			colorList =  CaseManagementUI.appStatelessBean.getPersistenceBean().getList(Color.class, false, "o.tag").getObject();
			colorDate = now;
		}
		return colorList;
	}
	
	private static List<ActualSituation> actualSituationList;
	private static  Date actualSituationDate;
	
	@SuppressWarnings("unchecked")
	public static  List<ActualSituation> getActualSituationList(boolean freshData) {
		Date now = new Date();
		if (actualSituationList == null || actualSituationDate == null || freshData){
			actualSituationList =  CaseManagementUI.appStatelessBean.getPersistenceBean().getList(ActualSituation.class, false, "o.tag").getObject();
			
			actualSituationDate = now;
			
		}
		int difHours = (int)((now.getTime() - actualSituationDate.getTime()) / (1000*60*60));
		if (difHours > 24){
			actualSituationList =  CaseManagementUI.appStatelessBean.getPersistenceBean().getList(ActualSituation.class, false, "o.tag").getObject();
			actualSituationDate = now;
		}
		return actualSituationList;
	}
	
	
	
	
	
	private static List<AppList> investigationStartedMethodList;
	private static  Date investigationStartedMethodDate;
	
	
	private static Map<AppListTypeEnum, List<AppList>> lists = new HashMap<AppListTypeEnum, List<AppList>>();
	private static Map<AppListTypeEnum, Date> dates = new HashMap<AppListTypeEnum, Date>();
	

	@SuppressWarnings("unchecked")
	public static  List<AppList> getList(AppListTypeEnum listType, boolean freshData) {
		Date now = new Date();
		if (lists.get(listType) == null || dates.get(listType) == null || freshData){
			lists.put(listType, CaseManagementUI.appStatelessBean.getPersistenceBean().getAppList(listType, true, true));
			dates.put(listType, now);
	
		}
		int difHours = (int)((now.getTime() - dates.get(listType).getTime()) / (1000*60*60));
		if (difHours > 24){
			lists.put(listType, CaseManagementUI.appStatelessBean.getPersistenceBean().getAppList(listType, true, true));
			dates.put(listType, now);
		}
		return lists.get(listType);
	}
	

}
